package com.rock.code.bigdata.kpihourtrend.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import com.rock.code.base.domain.AjaxJson;
import com.rock.code.base.domain.Constants;
import com.rock.code.common.exceptions.SystemException;
import com.rock.code.common.pager.Page;
import com.rock.code.common.excel.ExportExcel;

import com.rock.code.system.user.domain.TbSysLoginmsg;
import com.rock.code.base.controller.BaseController;
import com.rock.code.bigdata.kpihourtrend.domain.TbCdeKpitrend;
import com.rock.code.bigdata.kpihourtrend.service.TbCdeKpitrendService;

/**
* 指标趋势预测
*
* @author sunhaobo
* @date 2018/11/06
*/
@Controller
@RequestMapping("/tbCdeKpitrend")
public class TbCdeKpitrendController extends BaseController {

    /**
    * The tbCdeKpitrendServiceImpl
    */
    @Resource
    private TbCdeKpitrendService tbCdeKpitrendServiceImpl;

    /**
    * Dispatch.
    *
    * @return the string
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @RequestMapping("")
    public String dispatch() throws Exception {
        return "bigdata/kpihourtrend/tbCdeKpitrendList";
    }

    /**
    * queryPageList
    *
    * @return the page info
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/queryPageList", method = RequestMethod.POST)
    public PageInfo<TbCdeKpitrend> queryPageList() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        Page page = new Page(request);
        PageInfo pageInfo = null;
        try {
            param.put("isDelete", Constants.DELETE_N);
            pageInfo = tbCdeKpitrendServiceImpl.queryPageList(page, param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return pageInfo;
    }

    /**
    * querySingle
    *
    * @return TbCdeKpitrend
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/querySingle", method = RequestMethod.POST)
    public TbCdeKpitrend querySingle() throws Exception {
        Map<String, Object> param = this.getRequestMapSingle();
        TbCdeKpitrend tbCdeKpitrend = null;
        try {
            tbCdeKpitrend = tbCdeKpitrendServiceImpl.queryByParaMap(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return tbCdeKpitrend;
    }

    /**
    * Save.
    *
    * @param tbCdeKpitrend the TbCdeKpitrend
    * @return the ajax json
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxJson save(@RequestBody TbCdeKpitrend tbCdeKpitrend) throws Exception {
        try {
            TbSysLoginmsg sessionUser = getSessionUser();
            if (isNotEmpty(tbCdeKpitrend.getKpiId())) {
                tbCdeKpitrend.setModifier(sessionUser.getLoginCode());
                tbCdeKpitrend.setModifieDate(new Date());
                tbCdeKpitrendServiceImpl.update(tbCdeKpitrend);
            } else {
                tbCdeKpitrend.setIsDelete(Constants.DELETE_N);
                tbCdeKpitrend.setCreater(sessionUser.getLoginCode());
                tbCdeKpitrend.setCreateDate(new Date());
                tbCdeKpitrend.setModifier(sessionUser.getLoginCode());
                tbCdeKpitrend.setModifieDate(new Date());
                tbCdeKpitrendServiceImpl.insert(tbCdeKpitrend);
            }
            return ajaxJson;
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    /**
    * Delete.
    *
    * @param ids the ids
    * @return the ajax json
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public AjaxJson delete(@RequestParam String ids) throws Exception {
        try {
            String[] kpiIds = ids.split(",");
            TbSysLoginmsg sessionUser = getSessionUser();
            for (String kpiId : kpiIds) {
                TbCdeKpitrend tbCdeKpitrend = new TbCdeKpitrend();
                tbCdeKpitrend.setKpiId(kpiId);
                tbCdeKpitrend.setIsDelete(Constants.DELETE_Y);
                tbCdeKpitrend.setModifier(sessionUser.getLoginCode());
                tbCdeKpitrend.setModifieDate(new Date());
                tbCdeKpitrendServiceImpl.update(tbCdeKpitrend);
            }
            return ajaxJson;
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    /**
    * Exp excel.
    *
    * @return the ajax json
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/expExcel", method = RequestMethod.POST)
    public AjaxJson expExcel() throws Exception {
        try {
            Map<String, Object> param = this.getRequestMapSingle();
            String[] titles = (String[]) param.get("titles[]");
            String[] fields = (String[]) param.get("fields[]");
            Page page = new Page((String) param.get("sidx"), (String) param.get("sord"));
            param.put("isDelete", Constants.DELETE_N);
            ExportExcel ee = new ExportExcel();
            List rows = tbCdeKpitrendServiceImpl.queryListByOrder(page, param);
            String path = ee.exportExcel(request, response, 40000, titles, fields, rows, TbCdeKpitrend.class.getName());
            ajaxJson.setResult(path);
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return ajaxJson;
    }

    /**
    * Check input field.
    *
    * @param inputName the input name
    * @param inputValue the input value
    * @return the json object
    * @throws Exception the exception
    * @author sunhaobo
    * @date 2018/11/06
    */
    @ResponseBody
    @RequestMapping(value = "/checkInputField", method = RequestMethod.POST)
    public JSONObject checkInputField(String inputName, String inputValue) throws Exception {
        try {
            Map<String, Object> param = new HashMap<String, Object>();
            param.put(inputName, inputValue);
            param.put("isDelete", Constants.DELETE_N);
            List rows = tbCdeKpitrendServiceImpl.queryListByParaMap(param);
            if (rows != null && rows.size() > 0) {
                json.put("valid", false);
            } else {
                json.put("valid", true);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
        return json;
    }
}
